package com.qf.sk.controller;

import com.qf.sk.util.ZKConnUtil;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class SkController {

    @Autowired
    private CuratorFramework client;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private final String ORDER_KEY = "order";


//    http://localhost:8080/sk/jiaozi   买饺子
//    http://localhost:8080/sk/tangyuan 买汤圆

    // 超卖(库存100件,却卖出去大于100件商品),少卖(库存100件,只能卖出少于100件商品)
    // 购买饺子的操作
    @GetMapping("/sk/{item}")
    public String jiaozi(@PathVariable String item) throws Exception {
        // 获取锁和释放锁的基本对象
        InterProcessMutex lock = new InterProcessMutex(client, "/" + item);

        // 加锁
        if (lock.acquire(1000, TimeUnit.MILLISECONDS)) {
            // 成功获取锁资源的操作
            //1. 查询饺子的库存数量
            Integer stock = Integer.valueOf(redisTemplate.opsForValue().get(item));
            //2.1 如果库存数充足
            if (stock > 0) {
                //2.2 如果饺子的库存数大于1,给库存数 -1
                stock--;   // 线程不安全
                redisTemplate.opsForValue().set(item, stock + "");
                //2.3 给订单数 + 1
                Integer order = Integer.valueOf(redisTemplate.opsForValue().get(ORDER_KEY));
                order++;  // 线程不安全
                redisTemplate.opsForValue().set(ORDER_KEY, order + "");
                //2.4 返回信息,秒杀成功
                lock.release();   // 释放锁
                return "商品购买成功!!!!";
            } else {
                //3. 返回信息,秒杀失败
                lock.release();
                return "商品库存数不足,购买失败!!!";
            }
        } else {
            // 没有获取锁资源的操作
            return "没有获取到锁资源，凉凉~~~~";
        }
    }



    @GetMapping("/redis/sk/{item}")
    public String redisJiaoZi(@PathVariable String item) throws Exception {

        // Redis加锁  setnxex
        Boolean lockSuccess = redisTemplate.opsForValue().setIfAbsent(item + "lock", System.currentTimeMillis() + "", 3000, TimeUnit.MILLISECONDS);
        if(lockSuccess) {
            System.out.println("获取锁资源成功！！");
            //1. 查询饺子的库存数量
            Integer stock = Integer.valueOf(redisTemplate.opsForValue().get(item));
            //2.1 如果库存数充足
            if (stock > 0) {
                //2.2 如果饺子的库存数大于1,给库存数 -1
                stock--;   // 线程不安全
                redisTemplate.opsForValue().set(item, stock + "");
                //2.3 给订单数 + 1
                Integer order = Integer.valueOf(redisTemplate.opsForValue().get(ORDER_KEY));
                order++;  // 线程不安全
                redisTemplate.opsForValue().set(ORDER_KEY, order + "");
                //2.4 返回信息,秒杀成功
                //------------------------------ Redis释放锁资源 ----------------------------------
                redisTemplate.delete(item + "lock");
                return "商品购买成功!!!!";
            } else {
                //------------------------------ Redis释放锁资源 ----------------------------------
                redisTemplate.delete(item + "lock");
                return "商品库存数不足,购买失败!!!";
            }
        }else{
            System.out.println("获取锁资源失败！！");
            return "获取锁资源失败！！！！";
        }


        // Redisson(Redis)  -  curator(Zookeeper)
    }


}
